package com.ht.project.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ht.common.entity.PageInfo;
import com.ht.project.dao.PromanageDao;
import com.ht.right.dao.impl.BaseDaoImpl;
import com.ht.right.entity.ProamanageHeadModel;

public class PromanageDaoImpl extends BaseDaoImpl<ProamanageHeadModel> implements PromanageDao {

	@Override
	public List<ProamanageHeadModel> findChildProList(ProamanageHeadModel proamanageHeadModel) {
		String hql = "from ProamanageHeadModel t where t.uuid='"+proamanageHeadModel.getUuid()+"' order by t.uuid ";
		return super.find(hql);
	}

	@Override
	public List<ProamanageHeadModel> findAllProList() {
		String hql = "from ProamanageHeadModel t order by t.uuid ";
		return super.find(hql);
	}

	@Override
	public Map<String, Object> findListByProamanageIdForPage(ProamanageHeadModel proamanageHeadModel, PageInfo pageInfo) {
		StringBuffer hql = new StringBuffer("from ProamanageHeadModel p");
		StringBuffer hqlCount = new StringBuffer("select count(*) from ProamanageHeadModel p ");
		if (proamanageHeadModel!=null && !StringUtils.isEmpty(proamanageHeadModel.getProjectName())) {
			hql.append(" and p.projectName like '%" + proamanageHeadModel.getProjectName()+ "%' ");
			hqlCount.append(" and p.projectName like '%" + proamanageHeadModel.getProjectName()+ "%' ");
		}
		if (pageInfo.getSortField() != null && pageInfo.getSortField().length() > 0) {
			hql.append(" order by " + pageInfo.getSortField());
		}
		if (pageInfo.getSortOrder() != null && pageInfo.getSortOrder().length() > 0) {
			hql.append(" " + pageInfo.getSortOrder());
		}
		if (hql.indexOf("order by") == -1) {
			hql.append(" order by ");
		} else {
			hql.append(" , ");
		}
		hql.append(" p.uuid desc");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("total", super.findUnique(hqlCount.toString()));
		dataMap.put("data", super.find(hql.toString(), new String[] {}, pageInfo.getpageIndex(), pageInfo.getPageSize()));
		return dataMap;
	}

}
